# @Author ZhangGJ
# @Date 2021/12/07 11:43

import cv2.cv2 as cv2

# mac 级联分类器路径：/usr/local/lib/python3.9/site-packages/cv2/data

# 绘制红框
import numpy as np


def face():
    img = cv2.imread('../images/model.png')
    face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(img, 1.15)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 墨镜效果
def face2():
    face_img = cv2.imread('../images/model.png')
    glass_img = cv2.imread('../images/glass.png', cv2.IMREAD_UNCHANGED)
    height, width, channel = glass_img.shape
    face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalface_default.xml')
    gray_frame = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_frame, 1.15, 5)
    for (x, y, w, h) in faces:
        gw = w
        gh = int(height * w / width)
        glass_img = cv2.resize(glass_img, (gw, gh))
        _overlay_img(face_img, glass_img, x, y + int(h * 1 / 3))
    cv2.imshow('screen', face_img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def _overlay_img(img, img_over, img_over_x, img_over_y):
    img_h, img_w, img_p = img.shape
    img_over_h, img_over_w, img_over_c = img_over.shape
    if img_over_c == 3:
        img_over = cv2.cvtColor(img_over, cv2.COLOR_BGR2GRAY)
    for w in range(0, img_over_w):
        for h in range(0, img_over_h):
            if img_over[h, w, 3] != 0:
                for c in range(0, 3):
                    x = img_over_x + w
                    y = img_over_y + h
                    if x >= img_w or y >= img_h:
                        break
                    img[y, x, c] = img_over[h, w, c]
    return img


# 眼睛
def face3():
    img = cv2.imread('../images/model.png')
    eye_cascade = cv2.CascadeClassifier('../cascades/haarcascade_eye.xml')
    eyes = eye_cascade.detectMultiScale(img, 1.15)
    for (x, y, w, h) in eyes:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 猫
def face4():
    img = cv2.imread('../images/cat.jpg')
    cat_face_cascade = cv2.CascadeClassifier('../cascades/haarcascade_frontalcatface_extended.xml')
    cat_face = cat_face_cascade.detectMultiScale(img, 1.15, 4)
    for (x, y, w, h) in cat_face:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
    cv2.imshow('Where is your cat?', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 行人跟踪
def face5():
    img = cv2.imread('../images/monitoring.jpg')
    body_cascade = cv2.CascadeClassifier('../cascades/haarcascade_fullbody.xml')
    bodies = body_cascade.detectMultiScale(img, 1.15, 4)
    for (x, y, w, h) in bodies:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 车牌跟踪
def face6():
    img = cv2.imread('../images/car2.jpg')
    plate_cascade = cv2.CascadeClassifier("../cascades/haarcascade_russian_plate_number.xml")
    plates = plate_cascade.detectMultiScale(img, 1.15, 4)
    for (x, y, w, h) in plates:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 人脸识别
def face7():
    photos = list()
    lables = list()
    photos.append(cv2.imread('../images/face/summer1.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/summer2.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/summer3.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/Elvis1.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/Elvis2.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/Elvis3.png', 0))
    lables.append(1)
    names = {"0": "Summer", "1": "Elvis"}
    recognizer = cv2.face.EigenFaceRecognizer_create()
    recognizer.train(photos, np.array(lables))
    i = cv2.imread('../images/face/summer4.png', 0)
    label, confidence = recognizer.predict(i)
    print('Confidence = ' + str(confidence))
    print(names[str(label)])


def face8():
    photos = list()
    lables = list()
    photos.append(cv2.imread('../images/face/Mike1.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/Mike2.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/Mike3.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/kaikai1.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/kaikai2.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/kaikai3.png', 0))
    lables.append(1)
    names = {"0": "Mike", "1": "Kaikai"}
    recognizer = cv2.face.FisherFaceRecognizer_create()
    recognizer.train(photos, np.array(lables))
    i = cv2.imread('../images/face/Mike4.png', 0)
    label, confidence = recognizer.predict(i)
    print('Confidence = ' + str(confidence))
    print(names[str(label)])


def face9():
    photos = list()
    lables = list()
    photos.append(cv2.imread('../images/face/lxe1.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/lxe2.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/lxe3.png', 0))
    lables.append(0)
    photos.append(cv2.imread('../images/face/ruirui1.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/ruirui2.png', 0))
    lables.append(1)
    photos.append(cv2.imread('../images/face/ruirui3.png', 0))
    lables.append(1)
    names = {"0": "LXE", "1": "Ruirui"}
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    recognizer.train(photos, np.array(lables))
    i = cv2.imread('../images/face/ruirui4.png', 0)
    label, confidence = recognizer.predict(i)
    print('Confidence = ' + str(confidence))
    print(names[str(label)])


if __name__ == '__main__':
    face9()
